﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;
using System.Reflection;
using BrainTechLLC.ThreadSafeObjects;
using BrainTechLLC;
#if USE_HYPER
using Hyper.ComponentModel;
#endif

namespace BrainTechLLC.ThreadSafeObjects
{
#if NO_SILVERLIGHT
    [TypeDescriptionProvider(typeof(HyperTypeDescriptionProvider))]
#endif
    [Serializable]
    [DataContract]
    public class ThreadSafeLookupOnDemand<T1, T2> : ThreadSafeLookup<T1, T2> where T2 : class, new()
    {
        public bool Contains(T1 key)
        {
            return ContainsKey(key);
        }

        public T2 Get(T1 key)
        {
            T2 item = this[key];

            if (item == null)
            {
                item = new T2();
                AddOrSet(key, item);
            }

            return item;
        }

        public T2 Get(T1 key, Func<T1, T2> func)
        {
            T2 item = this[key];

            if (item == null)
            {
                item = func(key);
                AddOrSet(key, item);
            }

            return item;
        }
    }
}

